package com.example.myapplicationwork;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    // 存储默认密码，实际应用中应加密存储
    private static final String DEFAULT_PASSWORD = "123456";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        // 初始化SharedPreferences，用于存储密码修改记录
        initPasswordPreferences();

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();

                // 获取当前有效密码（可能是修改后的密码）
                String currentPassword = getCurrentPassword();

                if ("test".equals(username) && currentPassword.equals(password)) {
                    Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                    intent.putExtra("username", username);
                    startActivity(intent);
                    finish(); // 登录成功后关闭登录页面
                } else {
                    Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    // 初始化密码存储，设置默认密码
    private void initPasswordPreferences() {
        SharedPreferences sp = getSharedPreferences("password_record", MODE_PRIVATE);
        if (!sp.contains("current_password")) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("current_password", DEFAULT_PASSWORD);
            editor.putString("password_history", DEFAULT_PASSWORD + "(初始密码);");
            editor.apply();
        }
    }

    // 获取当前有效密码
    private String getCurrentPassword() {
        SharedPreferences sp = getSharedPreferences("password_record", MODE_PRIVATE);
        return sp.getString("current_password", DEFAULT_PASSWORD);
    }

    // 生命周期方法保持不变
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}
